<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of xy苹果助手
 *
 * @author sunday  modify by dragon.hu @2016.2.18
 */
require_once 'BaseChannel.php';

class Xy implements BaseChannel {

	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		$appKey = "";
		$UID = "";
		$res = $this->processVerify($UID, $appKey,ANDROID);
		echo $res;
	}

	function ios($data) {
		$appKey = "neo0IzZPiGueocoEkPV7ZNQ8K2i91T2x";
		$UID = "";
		$res = $this->processVerify($UID, $appKey,IOS);
		echo $res;
	}

	function processVerify($UID, $appKey,$platform) 
	{
		
		$receiveData = file_get_contents("php://input");
		//$responseData = json_decode($receiveData,true);
		$responseData = $_POST;
		 
		$orderLog = new ChargeOrder();
		$orderLog->log("xy", $_REQUEST);
		 
		$orderid = $responseData['orderid'];	//orderid
		$uid = $responseData['uid'];	//user id
		$serverid = $responseData['serverid'];	//服务器id
		$amount = $responseData['amount'];	//金额
		$extra = $responseData['extra'];	//客户端传递过去的订单
		$ts = $responseData['ts'];	//当前时间戳
		$sign = $responseData['sign'];	//签名
		 
		//先排序
		ksort($_POST);
		$query_string = array();
		foreach ($_POST as $key => $value)
		{
			if($key == 'sig'||$key == 'sign')
			{
				continue;
			}
			array_push($query_string, $key.'='.$value);
		}
		$query_string = join('&', $query_string);
		$mysign = md5($appKey.$query_string);

		$result = array();
		 
		if ($mysign == $sign) {

			$status = STATUS_SUCCESS;

			if(false == $orderLog->recordOrder($status, $amount, $extra, "IosXY", $platform,$orderid))
			{
				$result['ret'] = 7;
				$result['msg'] = "insert data error extra:$extra";
				$orderLog->log("xy","amount:".$amount." orderid:".$extra." platform:".$platform." failure");
			}else //success
			{
				$result['ret'] = 0;
				$result['msg'] = 'success';
				$orderLog->log("xy","amount:".$amount." orderid:".$extra." platform:".$platform." success");
			}

		}else
		{
			$result['ret'] = 6;
			$result['msg'] = 'sign error';
			$orderLog->log("xy","amount:".$amount." orderid:".$extra." platform:".$platform." sign error");
		}
		 
		 
		$res = json_encode($result);
		return $res;
		//print_r($result);
		//return urlencode($res);
	}

}

?>
